home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 676-700 / 682 / reqchange / extra / getfile.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  5KB  |  230 lines

  1. /*
  2. GetFile. Public Domain by Magnus Holmgren in 1992.
  3.  
  4. Compile with DICE:
  5. dcc -pr -2.0 -ms -proto -mRR -o GetFile GetFile.c
  6.  
  7. Note that in order to compile with other compilers, changes are neccessary.
  8. */
  9.  
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <exec/types.h>
  13. #include <exec/execbase.h>
  14. #include <exec/memory.h>
  15. #include <dos/rdargs.h>
  16. #include <dos/dos.h>
  17. #include <libraries/asl.h>
  18. #include <clib/asl_protos.h>
  19. #include <clib/dos_protos.h>
  20. #include <clib/exec_protos.h>
  21.  
  22. extern struct DOSBase *DOSBase;
  23. extern struct AslBase *AslBase;
  24. extern struct ExecBase *SysBase;
  25.  
  26. #define FILE    0
  27. #define TITLE   1
  28. #define VAR     2
  29. #define PATTERN 3
  30. #define GLOBAL  4
  31. #define NOFILES 5
  32. #define SAVE    6
  33. #define MULTI   7
  34.  
  35. VOID quotename( STRPTR );
  36.  
  37. VOID quotename( STRPTR name )
  38. {
  39.   if( strchr( name, ' ' ) )
  40.   {
  41.     LONG i;
  42.  
  43.     for( i = strlen( name ); i >= 0; i-- )
  44.       *( name + i + 1 ) = *( name + i );
  45.  
  46.     *name = '\"';
  47.     strcat( name, "\"" );
  48.   }
  49. }
  50.  
  51. _main()
  52. {
  53.   struct RDArgs *rdargs;
  54.   struct FileRequester *req = NULL;
  55.   STRPTR title, var, pos, pattern = NULL, buf = NULL;
  56.   LONG extflags = 0, funcflags = 0, len;
  57.  
  58.   static const TEXT VersTag[] = "$VER: GetFile 1.1 (19.03.92)";
  59.   static const TEXT OS2Required_Msg[] = "You need OS 2.04+\n";
  60.   static ULONG flags[ 8 ];
  61.   static TEXT fullpath[ 512 ], dir[ 256 ];
  62.  
  63.   LONG ret = RETURN_FAIL;
  64.  
  65.   dir[ 0 ] = fullpath[ 0 ] = '\0';
  66.  
  67.   if( SysBase -> LibNode . lib_Version < 37 )
  68.   {
  69.       Write( ( BPTR ) Output(),
  70.              OS2Required_Msg,
  71.              sizeof( OS2Required_Msg ) - 1 );
  72.       _exit( RETURN_FAIL );
  73.   }
  74.  
  75.   flags[ TITLE ] = "Select a file";
  76.   rdargs = ReadArgs( "FILE,TITLE,VAR,PATTERN,GLOBAL/S,NOFILES/S,"
  77.                      "SAVE/S,MULTISELECT/S", flags, NULL );
  78.  
  79.   if( rdargs == NULL )
  80.   {
  81.     PrintFault( IoErr(), "Error in argument line", );
  82.     _exit( RETURN_ERROR );
  83.   }
  84.  
  85.   if( flags[ FILE ] )
  86.     strncpy( fullpath, ( STRPTR ) flags[ FILE ], 511 );
  87.  
  88.   if( FilePart( fullpath ) == fullpath )
  89.   {
  90.     TEXT work[ 512 ];
  91.  
  92.     if( !GetCurrentDirName( work, 511 ) )
  93.     {
  94.       if( IoErr() == ERROR_OBJECT_WRONG_TYPE )
  95.         PutStr( "No CLI structure available\n" );
  96.       else
  97.         PutStr( "Error getting name of current directory\n" );
  98.  
  99.       goto fail;
  100.     }
  101.  
  102.     if( !AddPart( work, fullpath, 511 ) )
  103.       goto lenerr;
  104.  
  105.     strncpy( fullpath, work, 511 );
  106.   }
  107.  
  108.   if( !flags[ VAR ] || *( ( STRPTR ) flags[ VAR ] ) == '\0' )
  109.     flags[ VAR ] = "GetFileResult";
  110.  
  111.   var = ( STRPTR ) flags[ VAR ];
  112.   title = ( STRPTR ) flags[ TITLE ];
  113.  
  114.   if( flags[ SAVE ] )
  115.     funcflags = FILF_SAVE;
  116.   else
  117.     if( flags[ MULTI ] )
  118.       funcflags = FILF_MULTISELECT;
  119.  
  120.   if( flags[ PATTERN ] )
  121.   {
  122.     pattern = ( STRPTR ) flags[ PATTERN ];
  123.     funcflags |= FILF_PATGAD;
  124.   }
  125.  
  126.   if( flags[ NOFILES ] )
  127.     extflags = FIL1F_NOFILES;
  128.  
  129.   pos = PathPart( fullpath );
  130.   len = ( LONG ) pos - ( LONG ) fullpath;
  131.  
  132.   if( len > 0 )
  133.   {
  134.     if ( len > 255 )
  135.     {
  136.  
  137. lenerr:
  138.       PutStr( "Filename is too long" );
  139.       goto fail;
  140.     }
  141.  
  142.     strncpy( dir, fullpath, len );
  143.   }
  144.  
  145.   if( !( req = AllocAslRequestTags( ASL_FileRequest,
  146.                                     ASL_Hail, title,
  147.                                     ASL_File, FilePart( fullpath ),
  148.                                     ASL_Dir, dir,
  149.                                     ASL_FuncFlags, funcflags,
  150.                                     ASL_ExtFlags1, extflags,
  151.                                     ASL_Pattern, pattern,
  152.                                     TAG_END ) ) )
  153.   {
  154.     PutStr( "Couldn't allocate FileRequester structure\n" );
  155.     ret = RETURN_FAIL;
  156.     goto fail1;
  157.   }
  158.  
  159.   if( !RequestFile( req ) )
  160.   {
  161.     ret = RETURN_WARN;
  162.     goto fail;
  163.   }
  164.  
  165.   fullpath[ 0 ] = '\0';
  166.  
  167.   if( funcflags & FILF_MULTISELECT )
  168.   {
  169.     ULONG i, len = 0, dirlen = 0;
  170.  
  171.     dirlen = strlen( req -> rf_Dir );
  172.  
  173.     for( i = 0; i < req -> rf_NumArgs; i++ )
  174.       len += strlen( req -> rf_ArgList[ i ] . wa_Name ) + 3 + dirlen;
  175.  
  176.     if( !( buf = AllocVec( len + 1, MEMF_CLEAR | MEMF_PUBLIC ) ) )
  177.     {
  178.       PutStr( "Ran out of memory\n" );
  179.       ret = RETURN_FAIL;
  180.       goto fail;
  181.     }
  182.  
  183.     for( i = 0; i < req -> rf_NumArgs; i++ )
  184.     {
  185.       strncpy( fullpath, req -> rf_Dir, 511 );
  186.  
  187.       if( !AddPart( fullpath, req -> rf_ArgList[ i ] . wa_Name, 511 ) )
  188.         goto builderror;
  189.  
  190.       quotename( fullpath );
  191.       strcat( buf, fullpath );
  192.       strcat( buf, " " );
  193.     }
  194.   }
  195.   else
  196.   {
  197.     strncpy( fullpath, req -> rf_Dir, 511 );
  198.  
  199. builderror:
  200.     if( !AddPart( fullpath, req -> rf_File, 511 ) )
  201.     {
  202.       PutStr( "Buffer overflow building full filename\n" );
  203.       ret = RETURN_FAIL;
  204.       goto fail;
  205.     }
  206.  
  207.     quotename( fullpath );
  208.   }
  209.  
  210.   if( !SetVar( var,
  211.                buf ? buf : fullpath, -1,
  212.                flags[ GLOBAL ] ? GVF_GLOBAL_ONLY : GVF_LOCAL_ONLY ) )
  213.   {
  214.     PutStr( "Error setting result variable\n" );
  215.     ret = RETURN_FAIL;
  216.   }
  217.  
  218.   ret = RETURN_OK;
  219.  
  220. fail:
  221.  
  222.   FreeAslRequest( req );
  223.  
  224. fail1:
  225.  
  226.   FreeVec( buf );
  227.   FreeArgs( rdargs );
  228.   _exit( ret );
  229. }
  230.